package mo.tags.stack;

import java.util.Arrays;
import java.util.Stack;

public class L503 {

    public int[] nextGreaterElements(int[] nums) {
        int n = nums.length;
        int[] ret = new int[n];
        Arrays.fill(ret, -1);
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 2 * n -1; i++) {
            while (!stack.isEmpty() && nums[stack.peek()] < nums[i%n]) {
                ret[stack.pop()] = nums[i % n];
            }
            stack.push(i % n);
        }
        return ret;
    }

}
